<template>
  <div class="app-container">
    <!-- 页面头部信息 -->
    <div class="header-info">
      <h1 class="main-title">山西高考</h1>
      <h2 class="sub-title">点亮粉丝牌 刷1个墨镜 推荐"冲稳保"院校</h2>
      <p class="instruction">提问格式：选课组合+分数</p>
      
      <!-- 热气球图片和优先推荐文字 -->
      <div class="balloon-section">
        <img src="./qiqiu.png" alt="热气球" class="balloon-image" />
        <p class="balloon-text">优先推荐</p>
      </div>
    </div>

    <!-- 输入表单区域 -->
    <div class="form-section">
      <div class="form-row">
        <div class="form-item">
          <label>地区:</label>
          <el-input
            v-model="formData.province"
            placeholder="浙江"
            disabled  
            size="small" />
        </div>
        <div class="form-item">
          <label>等效分数:</label>
          <el-input-number
            v-model="formData.score"
            :min="0"
            :max="750"
            placeholder="444"
            size="small"
            :controls="false" />
        </div>
      </div>

      <div class="subjects-row">
        <label class="subjects-label">选科:</label>
        <div class="subjects-content">
          <div class="primary-subjects">
            <el-radio-group v-model="formData.primarySubject" size="small">
              <el-radio value="physics">物理</el-radio>
              <el-radio value="history">历史</el-radio>
            </el-radio-group>
          </div>
          <div class="secondary-subjects">
            <el-checkbox
              v-model="formData.chemistry"
              size="small"
              >化学</el-checkbox
            >
            <el-checkbox
              v-model="formData.biology"
              size="small"
              >生物</el-checkbox
            >
            <el-checkbox
              v-model="formData.politics"
              size="small"
              >政治</el-checkbox
            >
            <el-checkbox
              v-model="formData.geography"
              size="small"
              >地理</el-checkbox
            >
          
          </div>
        </div>
        <el-button
          type="danger"
          size="small"
          @click="handleQuery"
          :loading="loading"
          class="query-btn">
          查询
        </el-button>
      </div>
    </div>

    <!-- 结果展示区域 -->
    <div
      v-if="recommendations && Object.keys(recommendations).length > 0"
      class="results-section">
      <!-- 表头 -->
      <div class="results-header">
        <div class="level-column">层次</div>
        <div class="province-column">本省院校</div>
        <div class="province-column">外省院校</div>
      </div>

      <!-- 滚动内容区域 -->
      <div class="results-content">
        <!-- 冲刺院校 -->
        <div class="result-row rush-row">
          <div class="level-cell rush-level">
            <div class="level-name">冲</div>
            <div class="level-count">({{ (recommendations.rush?.local?.length || 0) + (recommendations.rush?.external?.length || 0) }})</div>
          </div>
          <div class="colleges-cell rush-colleges">
            <div
              v-if="recommendations.rush?.local?.length > 0"
              class="college-list">
              <div
                v-for="college in recommendations.rush.local"
                :key="college.college_code"
                class="college-item rush-item">
                <div class="college-name">{{ college.college_name }}</div>
              </div>
            </div>
            <div
              v-else
              class="no-colleges">
              没有符合条件的学校
            </div>
          </div>
          <div class="colleges-cell rush-colleges">
            <div
              v-if="recommendations.rush?.external?.length > 0"
              class="college-list">
              <div
                v-for="college in recommendations.rush.external"
                :key="college.college_code"
                class="college-item rush-item">
                <div class="college-name">{{ college.college_name }}</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 稳妥院校 -->
        <div class="result-row stable-row">
          <div class="level-cell stable-level">
            <div class="level-name">稳</div>
            <div class="level-count">({{ (recommendations.stable?.local?.length || 0) + (recommendations.stable?.external?.length || 0) }})</div>
          </div>
          <div class="colleges-cell stable-colleges">
            <div
              v-if="recommendations.stable?.local?.length > 0"
              class="college-list">
              <div
                v-for="college in recommendations.stable.local"
                :key="college.college_code"
                class="college-item stable-item">
                <div class="college-name">{{ college.college_name }}</div>
              </div>
            </div>
          </div>
          <div class="colleges-cell stable-colleges">
            <div
              v-if="recommendations.stable?.external?.length > 0"
              class="college-list">
              <div
                v-for="college in recommendations.stable.external"
                :key="college.college_code"
                class="college-item stable-item">
                <div class="college-name">{{ college.college_name }}</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 保底院校 -->
        <div class="result-row safe-row">
          <div class="level-cell safe-level">
            <div class="level-name">保</div>
            <div class="level-count">({{ (recommendations.safe?.local?.length || 0) + (recommendations.safe?.external?.length || 0) }})</div>
          </div>
          <div class="colleges-cell safe-colleges">
            <div
              v-if="recommendations.safe?.local?.length > 0"
              class="college-list">
              <div
                v-for="college in recommendations.safe.local"
                :key="college.college_code"
                class="college-item safe-item">
                <div class="college-name">{{ college.college_name }}</div>
              </div>
            </div>
          </div>
          <div class="colleges-cell safe-colleges">
            <div
              v-if="recommendations.safe?.external?.length > 0"
              class="college-list">
              <div
                v-for="college in recommendations.safe.external"
                :key="college.college_code"
                class="college-item safe-item">
                <div class="college-name">{{ college.college_name }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态提示 -->
    <div
      v-else-if="!loading && hasQueried"
      class="empty-state">
      <div class="empty-message">暂无推荐结果，请调整查询条件后重试</div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
// 学校数据
import schoolData from './school.json';

// 推荐算法
import { getRecommendations } from './utils/recommendation.js';

// 表单数据
const formData = reactive({
  province: '山西',
  score: 0,
  preferLocal: false,
  primarySubject: 'history', // 'physics' 或 'history'
  chemistry: true,
  biology: true,
  politics: true,
  geography: true,
  technology: false,
});

// 状态管理
const loading = ref(false);
const hasQueried = ref(false);
const recommendations = ref(null);


// 查询处理函数
const handleQuery = async () => {
  // 验证表单
  if (!formData.province) {
    ElMessage.warning('请输入地区');
    return;
  }
  if (!formData.score || formData.score <= 0) {
    ElMessage.warning('请输入有效的高考分数');
    return;
  }
  if (!formData.primarySubject) {
    ElMessage.warning('请选择物理或历史');
    return;
  }

  loading.value = true;
  hasQueried.value = true;

  try {
    // 组合所有选择的科目
    const allSubjects = [];
    // 添加必选的主科目
    if (formData.primarySubject === 'physics') allSubjects.push('物理');
    if (formData.primarySubject === 'history') allSubjects.push('历史');
    // 添加可选的副科目
    if (formData.chemistry) allSubjects.push('化学');
    if (formData.biology) allSubjects.push('生物');
    if (formData.politics) allSubjects.push('政治');
    if (formData.geography) allSubjects.push('地理');
    if (formData.technology) allSubjects.push('技术');

    // 调用修正后的推荐算法
    const result = getRecommendations({
      userProvince: formData.province,
      userScore: formData.score,
      subjects: allSubjects,
      schoolData: schoolData,
    });

    recommendations.value = result;

    // 统计推荐数量
    let totalCount = 0;
    Object.values(result).forEach((category) => {
      Object.values(category).forEach((provinceData) => {
        totalCount += provinceData.length;
      });
    });

    if (totalCount > 0) {
      ElMessage.success(`为您推荐了 ${totalCount} 所院校`);
    } else {
      ElMessage.info('暂无符合条件的院校，建议调整查询条件');
    }
  } catch (error) {
    console.error('推荐算法执行错误:', error);
    ElMessage.error('推荐过程中出现错误，请重试');
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 10px 10px 0 10px;
  font-family: Arial, sans-serif;
  max-width: 600px;
  margin: 0 auto;
  position: relative;

  // 手机端适配
  @media (max-width: 768px) {
    padding: 8px 8px 0 8px;
    max-width: 95%;
  }

  // 平板端适配
  @media (min-width: 768px) and (max-width: 1024px) {
    max-width: 80%;
    padding: 12px;
  }

  // 页面头部信息样式
  .header-info {
    background: white;
    border: 1px solid #ddd;
    padding: 20px;
    margin-bottom: 15px;
    border-radius: 4px;
    text-align: center;
    position: relative;

    // 手机端适配
    @media (max-width: 768px) {
      padding: 15px;
      margin-bottom: 12px;
    }

    .main-title {
      font-size: 28px;
      font-weight: bold;
      color: #d9534f;
      margin: 0 0 10px 0;
      line-height: 1.2;

      // 手机端调整
      @media (max-width: 768px) {
        font-size: 24px;
        margin-bottom: 8px;
      }
    }

    .sub-title {
      font-size: 18px;
      font-weight: 500;
      color: #337ab7;
      margin: 0 0 8px 0;
      line-height: 1.3;

      // 手机端调整
      @media (max-width: 768px) {
        font-size: 16px;
        margin-bottom: 6px;
      }
    }

    .instruction {
      font-size: 14px;
      color: #666;
      margin: 0;
      line-height: 1.4;

      // 手机端调整
      @media (max-width: 768px) {
        font-size: 13px;
      }
    }

    // 热气球区域样式
    .balloon-section {
      position: absolute;
      top: 10px;
      right: 15px;
      display: flex;
      flex-direction: column;
      align-items: center;

      // 手机端调整位置
      @media (max-width: 768px) {
        top: 8px;
        right: 10px;
      }

      .balloon-image {
        width: 50px;
        height: auto;
        margin-bottom: 3px;
        filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));

        // 手机端调整大小
        @media (max-width: 768px) {
          width: 40px;
          margin-bottom: 2px;
        }
      }

      .balloon-text {
        font-size: 10px;
        color: #d9534f;
        font-weight: 500;
        margin: 0;
        text-align: center;
        background: rgba(255, 255, 255, 0.9);
        padding: 1px 4px;
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

        // 手机端调整
        @media (max-width: 768px) {
          font-size: 9px;
          padding: 1px 3px;
          border-radius: 6px;
        }
      }
    }
  }

  .form-section {
    background: white;
    border: 1px solid #ddd;
    padding: 15px;
    margin-bottom: 5px;
    border-radius: 4px;

    // 手机端适配
    @media (max-width: 768px) {
      padding: 12px;
    }

    .form-row {
      display: flex;
      align-items: center;
      gap: 15px;
      margin-bottom: 10px;

      // 手机端垂直布局
      @media (max-width: 768px) {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
      }

      .form-item {
        display: flex;
        align-items: center;
        gap: 8px;

        // 手机端调整
        @media (max-width: 768px) {
          justify-content: space-between;
        }

        label {
          font-size: 14px;
          color: #333;
          white-space: nowrap;
          min-width: 50px;

          // 手机端调整
          @media (max-width: 768px) {
            font-size: 16px;
            min-width: 60px;
          }
        }

        :deep(.el-input) {
          width: 120px;

          // 手机端调整
          @media (max-width: 768px) {
            width: 150px;
          }

          .el-input__wrapper {
            box-shadow: 0 0 0 1px #1890ff inset;
          }
        }

        :deep(.el-input-number) {
          width: 120px;

          // 手机端调整
          @media (max-width: 768px) {
            width: 150px;
          }

          .el-input__wrapper {
            box-shadow: 0 0 0 1px #1890ff inset;
          }
        }
      }
    }

    .subjects-row {
      display: flex;
      align-items: flex-start;
      gap: 15px;

      // 手机端垂直布局
      @media (max-width: 768px) {
        flex-direction: column;
        gap: 10px;
      }

      .subjects-label {
        font-size: 14px;
        color: #333;
        white-space: nowrap;
        margin-top: 2px;

        // 手机端调整
        @media (max-width: 768px) {
          font-size: 16px;
          margin-top: 0;
        }
      }

      .subjects-content {
        display: flex;
        gap: 15px;
        flex: 1;
        flex-wrap: wrap;

        // 手机端调整
        @media (max-width: 768px) {
          flex-direction: column;
          gap: 8px;
        }

        .primary-subjects,
        .secondary-subjects {
          display: flex;
          gap: 10px;
          flex-wrap: wrap;

          // 手机端调整
          @media (max-width: 768px) {
            gap: 8px;
          }
        }

        :deep(.el-radio__label) {
          font-size: 14px;
          color: #333;

          // 手机端调整
          @media (max-width: 768px) {
            font-size: 16px;
          }
        }

        :deep(.el-checkbox__label) {
          font-size: 14px;
          color: #333;

          // 手机端调整
          @media (max-width: 768px) {
            font-size: 16px;
          }
        }

        :deep(.el-radio) {
          margin-right: 15px;
          
          // 手机端增大点击区域
          @media (max-width: 768px) {
            margin-right: 12px;

            .el-radio__input {
              .el-radio__inner {
                width: 16px;
                height: 16px;
              }
            }
          }
        }

        :deep(.el-checkbox) {
          // 手机端增大点击区域
          @media (max-width: 768px) {
            margin-right: 12px;

            .el-checkbox__input {
              .el-checkbox__inner {
                width: 16px;
                height: 16px;
              }
            }
          }
        }
      }

      .query-btn {
        background: #d9534f;
        border-color: #d43f3a;
        color: white;
        padding: 5px 20px;
        font-size: 14px;
        white-space: nowrap;

        // 手机端调整
        @media (max-width: 768px) {
          padding: 8px 24px;
          font-size: 16px;
          width: 100%;
          margin-top: 5px;
        }
      }
    }
  }

  .results-section {
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
    overflow: hidden;
    max-height: calc(100vh - 140px);
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;

    // 手机端调整高度
    @media (max-width: 768px) {
      max-height: calc(100vh - 56vh);
    }

    .results-header {
      display: flex;
      background: #337ab7;
      color: white;
      font-weight: bold;
      flex-shrink: 0;

      .level-column {
        width: 80px;
        padding: 10px;
        text-align: center;
        border-right: 1px solid #2e6da4;
        font-size: 14px;

        // 手机端调整
        @media (max-width: 768px) {
          width: 60px;
          padding: 8px 4px;
          font-size: 12px;
        }
      }

      .province-column {
        flex: 1;
        padding: 10px;
        text-align: center;
        border-right: 1px solid #2e6da4;
        font-size: 14px;

        // 手机端调整
        @media (max-width: 768px) {
          padding: 8px 4px;
          font-size: 12px;
        }

        &:last-child {
          border-right: none;
        }
      }


    }

    .results-content {
      flex: 1;
      overflow-y: auto;
      scrollbar-gutter: stable;
      min-height: 150px;

      // 手机端优化滚动
      @media (max-width: 768px) {
        -webkit-overflow-scrolling: touch;
        min-height: 100px;
        /* 移动端滚动指示器 */
        scrollbar-width: thin;
        scrollbar-color: #999 transparent;
        
        /* Webkit滚动条样式 */
        &::-webkit-scrollbar {
          width: 6px;
        }
        
        &::-webkit-scrollbar-track {
          background: rgba(0, 0, 0, 0.1);
          border-radius: 3px;
        }
        
        &::-webkit-scrollbar-thumb {
          background: #999;
          border-radius: 3px;
        }
        
        /* 滚动区域边界提示 - 简化样式 */
        border-top: 1px solid rgba(0, 0, 0, 0.05);
      }
    }

    .result-row {
      display: flex;
      border-bottom: 1px solid #ddd;

      &:last-child {
        border-bottom: none;
      }

      .level-cell {
        width: 80px;
        padding: 15px 5px;
        text-align: center;
        border-right: 1px solid #ddd;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;

        // 手机端调整
        @media (max-width: 768px) {
          width: 60px;
          padding: 10px 3px;
        }

        .level-name {
          font-size: 24px;
          font-weight: bold;
          margin-bottom: 5px;

          // 手机端调整
          @media (max-width: 768px) {
            font-size: 18px;
            margin-bottom: 3px;
          }
        }

        .level-count {
          font-size: 12px;
          color: #666;

          // 手机端调整
          @media (max-width: 768px) {
            font-size: 10px;
          }
        }

        &.rush-level .level-name {
          color: #d9534f;
        }

        &.stable-level .level-name {
          color: #337ab7;
        }

        &.safe-level .level-name {
          color: #5cb85c;
        }
      }

      .colleges-cell {
        flex: 1;
        padding: 10px;
        border-right: 1px solid #ddd;

        // 手机端调整
        @media (max-width: 768px) {
          padding: 8px 6px;
        }

        &:last-child {
          border-right: none;
        }

        &.rush-colleges {
          background-color: rgba(217, 83, 79, 0.05);
        }

        &.stable-colleges {
          background-color: rgba(51, 122, 183, 0.05);
        }

        &.safe-colleges {
          background-color: rgba(92, 184, 92, 0.05);
        }

        .college-list {
          .college-item {
            padding: 8px 0;
            line-height: 1.4;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);

            // 手机端调整
            @media (max-width: 768px) {
              padding: 6px 0;
              line-height: 1.3;
            }

            &:last-child {
              border-bottom: none;
            }

            .college-name {
              font-size: 14px;
              color: #333;
              word-break: break-all;

              // 手机端调整
              @media (max-width: 768px) {
                font-size: 12px;
              }
            }

            &.rush-item {
              .college-name {
                color: #d9534f;
                font-weight: 500;
              }
            }

            &.stable-item {
              .college-name {
                color: #337ab7;
                font-weight: 500;
              }
            }

            &.safe-item {
              .college-name {
                color: #5cb85c;
                font-weight: 500;
              }
            }
          }
        }

        .no-colleges {
          color: #999;
          font-style: italic;
          text-align: center;
          padding: 20px;
          font-size: 14px;

          // 手机端调整
          @media (max-width: 768px) {
            padding: 15px 5px;
            font-size: 12px;
          }
        }
      }
    }
  }

  .empty-state {
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 40px;
    text-align: center;

    // 手机端调整
    @media (max-width: 768px) {
      padding: 30px 20px;
    }

    .empty-message {
      color: #999;
      font-size: 14px;

      // 手机端调整
      @media (max-width: 768px) {
        font-size: 16px;
      }
    }
  }
}
</style>
